package com.eastjet.equipment.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eastjet.equipment.mapper.DeviceFrrMapper;
import com.eastjet.equipment.service.DeviceFrrService;
import com.eastjet.equipment.service.DeviceRulesService;
import com.eastjet.equipment.util.CodeRules;
import com.eastjet.equipment.util.DateTimeUtil;
import com.eastjet.equipment.util.PageUtil;
import com.eastjet.equipment.util.RedisUtil;
import com.eastjet.maintenance.domain.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author 杨博文
* @description 针对表【device_frr(故障报修表)】的数据库操作Service实现
* @createDate 2024-11-25 09:23:37
*/
@Service
public class DeviceFrrServiceImpl extends ServiceImpl<DeviceFrrMapper, DeviceFrr>
    implements DeviceFrrService {

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private DeviceFrrMapper deviceFrrMapper;
    @Autowired
    private DeviceRulesService deviceRulesService;

    @Autowired
    private CodeRules codeRules;

    @Override
    public Page<DeviceFrr> getDeviceFrrList(int page, int size, Map<String, Object> map) {
        // 定义Redis中的键
        String redisKey = "DeviceFrrList:";
        System.out.println("DeviceFrrList-deviceFrr:"+map);
        // 先查缓存Redis
        if (redisUtil.hasKey(redisKey)) {
            System.out.println(1);
            System.out.println("DeviceFrrList-从Redis中获取数据");
            List<DeviceFrr> DeviceFrrList;
            if (map != null && map.size() > 0 ) {
                System.out.println(2);
                DeviceFrrList = deviceFrrMapper.getFrrByCondition(map); // 获取所有数据
            } else {
                System.out.println(3);
                DeviceFrrList   = (List<DeviceFrr>) redisUtil.get(redisKey);

            }

            DeviceFrrList = DeviceFrrList.stream().map((item) -> {
                item.setFrrTime(DateTimeUtil.convertToDate(item.getFrrTime()));
                item.setFrrBadtime(DateTimeUtil.convertToDate(item.getFrrBadtime()));
                return item;
            }).collect(Collectors.toList());
            System.out.println("DeviceFrrList-condition:"+DeviceFrrList);
        return PageUtil.paginate(DeviceFrrList, page, size);
        } else {
            System.out.println(4);
            System.out.println("在数据库中查询DeviceFrrList");
            List<DeviceFrr> DeviceFrrList;
            // 根据condition条件查询
            if (map != null && map.size() > 0) {
                System.out.println(5);

                DeviceFrrList = deviceFrrMapper.getFrrByCondition(map);

            } else {
                System.out.println(6);
                DeviceFrrList = deviceFrrMapper.getFrrByCondition(map); // 获取所有数据

                // 将数据存入Redis
                redisUtil.set(redisKey, DeviceFrrList);
            }

            DeviceFrrList = DeviceFrrList.stream().map((item) -> {
                item.setFrrTime(DateTimeUtil.convertToDate(item.getFrrTime()));
                item.setFrrBadtime(DateTimeUtil.convertToDate(item.getFrrBadtime()));
                return item;
            }).collect(Collectors.toList());
            System.out.println("DeviceFrrList-condition:"+DeviceFrrList);
            // 使用PageUtil进行分页
            return PageUtil.paginate(DeviceFrrList, page, size);
        }
    }

    @Override
    public String addDeviceFrr(DeviceFrr DeviceFrr,String TableName) {
        System.out.println("DeviceFrr:" + DeviceFrr.toString());
        List<DeviceRules> list= deviceRulesService.getDeviceRulesByTableName(TableName);
        if (list.size() == 0)
        {
            //返回错误信息
            return "DeviceRules Table is not have the TableName";
        }else
        {
            System.out.println("1564654654654564654564654564564");
            // 调用代码规则
            DeviceFrr.setCode(codeRules.codeRules(list));
        }
        try {
            System.out.println("更新设备维修经验库");
            // 尝试将数据插入数据库
            if (deviceFrrMapper.insertDeviceFrr(DeviceFrr) > 0) {
                // 定义Redis的键
                String redisKey = "DeviceFrrList:";
                try {
                    // 从数据库获取当前所有数据
                    List<DeviceFrr> DeviceFrrList = deviceFrrMapper.getFrrByCondition(null); // 可以根据需要替换为实际查询条件
                    // 将更新后的数据写入Redis
                    redisUtil.set(redisKey, DeviceFrrList);
                } catch (Exception e) {
                    // 处理Redis操作异常
                    System.err.println("Redis更新失败: " + e.getMessage());
                    // 这里可以根据实际情况决定是否要抛出异常或者进行其他处理
                }
                return "添加成功";
            } else {
                return "添加失败";
            }
        } catch (Exception e) {
            // 处理数据库操作异常
            System.err.println("数据库操作失败: " + e.getMessage());
            return "添加失败";
        }
    }

    @Override
    public String deleteDevicFrr(DeviceFrr DeviceFrr) {
        System.out.println("DeviceFrr:" + DeviceFrr.toString());
        try {
            // 尝试将数据插入数据库
            if (deviceFrrMapper.deleteDeviceFrr(DeviceFrr) > 0) {
                // 定义Redis的键
                String redisKey = "DeviceFrrList:";
                try {
                    // 从数据库获取当前所有数据
                    List<DeviceFrr> DeviceFrrList = deviceFrrMapper.getFrrByCondition(null); // 可以根据需要替换为实际查询条件
                    // 将更新后的数据写入Redis
                    redisUtil.set(redisKey, DeviceFrrList);
                } catch (Exception e) {
                    // 处理Redis操作异常
                    System.err.println("Redis更新失败: " + e.getMessage());
                    // 这里可以根据实际情况决定是否要抛出异常或者进行其他处理
                }
                return "删除成功";
            } else {
                return "删除失败";
            }
        } catch (Exception e) {
            // 处理数据库操作异常
            System.err.println("数据库操作失败: " + e.getMessage());
            return "删除失败";
        }
    }

    @Override
    public List<DeviceUser> getDeviceUserList() {
        return deviceFrrMapper.getUsers();
    }

    @Override
    public List<DeviceUser> getDeptAddPhone(DeviceUser deviceUser) {
        return deviceFrrMapper.getDeptAddPhone(deviceUser);
    }

    @Override
    public List<Equipment> getEquipmentList(String condition) {
        return deviceFrrMapper.getEquipmentList(condition);
    }
}




